<template>
    <div class="m-admin-admin-list">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title a m-cf">菜单表</div>
                        </div>
                        <div class="widget-body">
                            <!-- 工具栏 -->
                            <div class="page_toolbar am-margin-bottom-xs">
                                <div class="am-form-group">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <a
                                            @click="openModal('add')"
                                            class="am-btn am-btn-default am-btn-success"
                                        >
                                            <span class="am-icon-plus"></span> 新增
                                        </a>
                                    </div>
                                </div>
                            </div>
                            <div class="am-scrollable-horizontal">
                                <el-table
                                    stripe
                                    :data="list"
                                    row-class-name="m-table-row"
                                    style="width: 100%">
                                    <el-table-column
                                        prop="menu_id"
                                        label="ID"
                                        width="80">
                                    </el-table-column>
                                    <el-table-column
                                        prop="menu_title"
                                        label="菜单标题"
                                    >
                                        <template slot-scope="scope">
                                            <span v-if="scope.row.level > 1">
                                                <span v-for="(item, index) in (+scope.row.level - 1)" :key="index">&nbsp;&nbsp;&nbsp;</span>
                                                ├&nbsp;
                                            </span>
                                            {{scope.row.menu_title}}
                                        </template>
                                    </el-table-column>
                                    <el-table-column
                                        prop="menu_path"
                                        label="菜单地址"
                                        width="200"
                                    >
                                    </el-table-column>
                                    <el-table-column
                                        prop="menu_icon"
                                        label="菜单图标"
                                        width="100"
                                    >
                                    </el-table-column>
                                    <el-table-column
                                        prop="menu_sort"
                                        label="排序"
                                        width="100"
                                    >
                                        <template slot-scope="scope">
                                            <input-edit :val="scope.row.menu_sort" :item="scope.row" @blurInput="tableEdit"></input-edit>
                                        </template>
                                    </el-table-column>
                                    <el-table-column
                                        fixed="right"
                                        label="操作"
                                        width="200"
                                    >
                                        <template slot-scope="scope">
                                            <el-button type="primary" size="mini" plain icon="el-icon-edit" @click="openModal('edit', scope.row)">编辑</el-button>
                                            <el-button type="danger" size="mini" @click="delItem(scope.row)" plain icon="el-icon-delete">删除</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <el-dialog title="添加菜单" :visible.sync="dialogFormVisible" :width="'500px'">
            <el-form :model="form">
                <el-form-item label="菜单名称" :label-width="formLabelWidth">
                    <el-input v-model="form.menu_title"></el-input>
                </el-form-item>
                <el-form-item label="菜单name" :label-width="formLabelWidth">
                    <el-input v-model="form.menu_name"></el-input>
                </el-form-item>
                <el-form-item label="菜单path" :label-width="formLabelWidth">
                    <el-input v-model="form.menu_path"></el-input>
                </el-form-item>
                <el-form-item label="菜单图标" :label-width="formLabelWidth">
                    <el-input v-model="form.menu_icon"></el-input>
                </el-form-item>
                <el-form-item label="菜单排序" :label-width="formLabelWidth">
                    <el-input v-model="form.menu_sort"></el-input>
                </el-form-item>
                <el-form-item label="上级菜单" :label-width="formLabelWidth">
                    <el-select v-model="form.menu_pid" placeholder="请选择上级菜单">
                        <el-option :label="'根目录'" :value="0"></el-option>
                        <el-option v-for="(item, index) in list" :key="index" :label="item.menu_title" :value="item.menu_id">
                                <span v-if="item.level > 1">
                                    <span v-for="(item, index2) in (+item.level - 1)" :key="index2">&nbsp;&nbsp;&nbsp;</span>
                                    ├&nbsp;
                                </span>
                                {{item.menu_title}}
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveModal">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import { list as menuList, add as menuAdd, del as menuDel } from '../../../api/system/menu.js';
import inputEdit from '../../../comp/inputEdit.vue';


export default {
    name: 'adminList',
    components: {
        inputEdit
    },
    data () {
        return {
            list: [],
            formLabelWidth: '100px',
            form: {
                menu_id: '',
                menu_title: '',
                menu_name: '',
                menu_path: '',
                menu_icon: '',
                menu_pid: '',
                menu_sort: ''
            },
            dialogFormVisible: false
        }
    },
    mounted () {
        this.getList();
    },
    methods: {
        getList () {
            menuList({menu_pid: ''}).then(res => {
                if (res.errno === 0) {
                    this.list = res.data.list;
                    console.log(this.list)
                }
            });
        },
        openModal (type, item = {}) {
            if (type === 'add') {
                //  清空
                this.formNull();
            } else if (type === 'edit') {
                this.formNull();
                this.form = Object.assign(this.form, item);
            }
            this.dialogFormVisible = !this.dialogFormVisible;
        },
        //  清空表单
        formNull () {
            this.form = {
                menu_id: '',
                menu_title: '',
                menu_name: '',
                menu_path: '',
                menu_icon: '',
                menu_pid: '',
                menu_sort: ''
            }
        },
        saveModal () {
            menuAdd(this.form).then(res => {
                if (res.errno === 0) {
                    this.$message({
                        type: 'success',
                        message: res.errmsg || '添加成功'
                    });
                    this.getList();
                    this.dialogFormVisible = false;
                } else {
                    this.$message({
                        type: 'info',
                        message: res.errmsg || '添加失败'
                    });  
                }
            });
        },
        del (item) {
            menuDel(item).then(res => {
                if (res.errno === 0) {
                    this.$message({
                        type: 'success',
                        message: res.errmsg || '删除成功'
                    });
                    this.getList();
                } else {
                    this.$message({
                        type: 'info',
                        message: res.errmsg || '删除失败'
                    });  
                }
            });
        },
        delItem (item) {
            this.$confirm('此操作将删除该条记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del(item);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        tableEdit ({val, item}) {
            this.form = Object.assign(this.form, item);
            this.form.menu_sort = val;
            this.saveModal();
        }
    }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.m-admin-admin-list{

}
</style>
